package com.zh.webcommon.common.query.mp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zh.webcommon.common.param.CommonParam;

import java.time.LocalDateTime;

/**
 * 继承的这个抽象类，推荐使用单例。
 * 参考 {@link NoneConditionStrategy} 类的实现。
 *
 * @author Zhou
 * @date 9:32 2025/7/3
 */
public abstract class AbstractConditionStrategy<T, Q extends CommonParam> {
    static final String FILED_CREATE_TIME = "createTime";

    //    private final Q condition;
//
//    protected AbstractConditionStrategy(Q condition) {
//        this.condition = condition;
//    }

//    public final Q condition() {
//        return this.condition;
//    }

    public QueryWrapper<T> getQueryWrapper(Q condition) {
        QueryWrapper<T> query = new QueryWrapper<>();

        /*处理各自的查询条件*/
        specification(query, condition);

        /*处理通用的查询条件*/

        //创建时间范围
        LocalDateTime startTime = condition.getStartTime();
        LocalDateTime endTime = condition.getEndTime();
        if (startTime != null && endTime != null) {
            query.between(FILED_CREATE_TIME, startTime, endTime);
        }

        //字段排序
        var sortField = condition.getSortField();
        if (sortField != null && sortField.isBlank()) {
            query.orderBy(true, condition.isAsc(), sortField);
        }

        return query;
    }

    abstract protected void specification(QueryWrapper<T> queryWrapper, Q param);
}
